$(function(){
	$("#txtTitle").live("focusin", function(){
		$("#divWarn").html('');
	});
	
	$("#txtKeyword").live("focusin", function(){
		$("#divWarn").html('');
	});
	
	$("#myEditor").live("focusin", function(){
		$("#divWarn").html('');
	});
});
function submitShare(){
	if(validate()){
		if(isLogin()){
			saveShare();			
		}else{
			loginLayer();
		}
	}	
}

function textTrim(txt) {  
	return txt.replace(/(^\s*)|(\s*$)/g, "");  
}

function saveShare(){
	var data = getShare();
	$.ajax({
		type: "post",
		dataType: "text",
		contentType: "application/json",
		data: data,
		url: ctx + "/share.do?action=0",
		success: function(v){
			if(v == "0"){
				layer.alert("分享失败");
			}else if(v == "2"){
				layer.alert("亲美美，请登录后再提交");
			}else{
				$("#passwordShow").val('');
				$("#email").val('');
				window.location.href = "share/"+ v + ".html";
			}
		}
	});
}

function getShare(){
	var obj = new Object();
	var title = $("#txtTitle").val();
	obj.title = title;
	var keyword = "美乐车";	
	obj.keyword = keyword;
	var content = um.getContent();
	obj.content = content;
	
	var json = JSON.stringify(obj);
	
	return json;
}

function validate(){
	var title = $("#txtTitle").val();
	if(textTrim(title) == ""){
		warn("请输入分享标题");
		return false;
	}
	
	var keyword = $("#txtKeyword").val();
	if(keyword == "" || null == keyword){
		warn("请输入分享关键字");
		return false;
	}
	
	var content = um.getContent();
	if(textTrim(content) == "" || null == content || content == "<p>&nbsp; &nbsp;		</p>"){

		warn("请输入分享内容");
		return false;
	}
	
	return true;
}

function warn(tip){
	$("#divWarn").html("<img src=\""+ctx+"/image/warn.png\"/>" + tip + "").css("height", "40px;").css("background-color", "#9bffa3");
	$("#divWarn").css("color", "red").css("margin", "5px 0px 5px 0px").css("height", "60px;");
}
 
	
function isLogin(){
	var bool = false;
	$.ajax({
		type : 'post',
		dataType : 'text',
		contentType : "application/json",
		url: "login.do",
		async: false,
		success: function(v){
			if(v == "true"){
				//登陆
				bool = true;
			}
		}
	});
	
	return bool;
}

var pageLogin;
function loginLayer(){
	pageLogin = $.layer({
	    type: 1,
	    title: '美乐车用户登陆',
	    area: ['auto', 'auto'],
	    border: [0], //去掉默认边框
	    shade: [0.5, '#000', true],
	    closeBtn: [0, true], //去掉默认关闭按钮
	    shift: 'top', //从左动画弹出
	    page: { dom: '#div_login'}
	});
}

function login(){
	var userName = document.getElementById("email").value;
	var password = $("#passwordShow").val();
	if(userName == ""){
		layer.alert("请输入邮箱/手机号/用户名", 8);
		return;
	}
	
	if(password == ""){
		layer.alert("请输入登陆密码", 8);
		return;
	}
	//发登陆请求
	$.ajax({
		type: "post",
		dataType: "text",
		url: ctx + "/u.do?type=10&userName="+userName+"&password="+password,
		success: function(v){
			if(v != ""){
				layer.close(pageLogin);
				saveShare();
			}else{
				layer.alert("亲美美，用户名或者密码有误", 8);
			}
		}
	}); 
	
}